var fs= require('fs');
var path = require('path');

//  首字母大写
function toUpper(str){
    var arr = str.split("");
    arr[0] = arr[0].toUpperCase();
    return arr.join("");
}

//  首字母小写
function toLower(str){
    var arr = str.split("");
    arr[0] = arr[0].toLowerCase();
    return arr.join("");
}

//  替换 {{A!xxx}}
function upperParam(file,param,value){
    value = toUpper(params[param])
    f = file.replaceAll("{{A!"+param+"}}",value)
    // f = file.replaceAll("Demo","{{A!model_name}}")
    return f
}

//  替换 {{a!xxx}}
function lowerParam(file,param,value){
    value = toLower(params[param])
    f = file.replaceAll("{{a!"+param+"}}",value)
    // f = file.replaceAll("demo","{{a!model_name}}")
    return f
}

String.prototype.replaceAll = function(s1,s2){
    return this.replace(new RegExp(s1,"gm"),s2);
}

//  trans
function trans(content,params){

    for(var param in params){ 
        content = upperParam(content,param,params[param])
        content = lowerParam(content,param,params[param])  
    }

    fs.writeFile("test.go",content,'utf8',function(err){  
        if(err){  
            console.log(err);
        }
    });
}

var params = {
    model_name : "SpiderItem"
}

var file = fs.readFileSync("template_test.tp", "utf8");
trans(file,params)
console.log("finish")
  
